<template>
  <header>
    <div class="main">
      <div class="logo" @click="backHome">
        <span class="sun"></span>
        <span class="text">天气系统</span>
      </div>
      <div class="data" v-if="weatherStore.nowWeather">
        <span class="city">{{ weatherStore.cityName }}</span>
        <span>实时天气 :</span>
        <span>{{ weatherStore.nowWeather.weather }}</span>
        <span>{{ weatherStore.nowWeather.temperature }}℃</span>
        <span
          >{{ weatherStore.nowWeather.winddirection }}风{{
            weatherStore.nowWeather.windpower
          }}级</span
        >
      </div>
      <div class="clicks">
        <div class="i" @click="flag = true">i</div>
        <div class="addCity" v-show="isShow" @click="addCity"></div>
      </div>
    </div>
  </header>
  <Transition name="prompt">
    <div class="promptData" v-if="flag">
      <div>
        <h3>关于:</h3>
        <span>这个应用可以用来追踪你选择城市的当前天气</span>
      </div>
      <div>
        <h3>如何使用:</h3>
        <span>1.点击搜索框输入你希望追踪的城市</span>
        <span>2.在搜索结果中选中一个城市，你将获取当地最新的天气</span>
        <span>3.点击右侧的＋号可以将追踪城市的天气情况保存在首页</span>
      </div>
      <div>
        <h3>移除城市:</h3>
        <span>如果你不想在首页关注某个城市,可以通过底部的按钮删除它</span>
      </div>
      <button @click="flag = false">关闭</button>
    </div>
  </Transition>

  <RouterView />
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useWeatherStore } from '../stores/WeatherStore.js'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const cityObj = ref({})
const router = useRouter()
const route = useRoute()
import { getWeather } from '@/api/weather'
const flag = ref(false)
const weatherStore = useWeatherStore()
const backHome = () => {
  router.push('/homeMain')
}

const addCity = async () => {
  try {
    const weatherData = await getWeather(route.params.key, 'base')
    cityObj.value = {
      city: route.query.search,
      adcode: route.params.key,
      weather: weatherData.lives[0],
    }
    weatherStore.addCity(cityObj.value)
    alert('城市添加成功:', cityObj.value)
  } catch (error) {
    alert('添加城市失败:', error)
  }
}
const isShow = computed(() => {
  return (
    route.path !== '/homeMain' &&
    !weatherStore.cityList.some((city) => city.adcode === route.params.key)
  )
})
onMounted(async () => {
  await weatherStore.getCurrentCityWeather()
})
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  height: 100vh;
  background: #2e6487;
}
header {
  height: 100px;
  color: white;
  box-shadow: 0 10px 10px rgba(0, 0, 0, 0.3);
  .main {
    position: relative;
    display: flex;
    align-items: center;
    gap: 20px;
    width: 1300px;
    height: 100%;
    margin: 0 auto;
    .logo {
      display: flex;
      align-items: center;
      gap: 10px;
      // width: 200px;
      height: 100%;
      cursor: pointer;
      .sun {
        width: 30px;
        height: 30px;
        background: white;
        clip-path: polygon(
          100% 50%,
          74.15% 56.47%,
          93.3% 75%,
          67.68% 67.68%,
          75% 93.3%,
          56.47% 74.15%,

          50% 100%,
          43.53% 74.15%,
          25% 93.3%,
          32.32% 67.68%,
          6.7% 75%,
          25.85% 56.47%,

          0% 50%,
          25.85% 43.53%,
          6.7% 25%,
          32.32% 32.32%,
          25% 6.7%,
          43.53% 25.85%,

          50% 0%,
          56.47% 25.85%,
          75% 6.7%,
          67.68% 32.32%,
          93.3% 25%,
          74.15% 43.53%
        );
      }
      .text {
        font-size: 20px;
        font-weight: 600;
      }
    }
    .data {
      display: flex;
      align-items: center;
      gap: 10px;
      height: 100%;
    }
    .city {
      font-size: 18px;
      font-weight: 600;
    }
    .clicks {
      position: absolute;
      right: 0;
      display: flex;
      justify-content: end;
      align-items: center;
      gap: 10px;
      width: 200px;
      height: 100%;
      .i {
        width: 30px;
        height: 30px;
        font-size: 25px;
        font-weight: 600;
        color: white;
        line-height: 30px;
        text-align: center;
        border-radius: 50%;
        border: 2px solid white;
        cursor: pointer;
      }
      .addCity {
        clip-path: polygon(
          0 40%,
          40% 40%,
          40% 0,
          60% 0,
          60% 40%,
          100% 40%,
          100% 60%,
          60% 60%,
          60% 100%,
          40% 100%,
          40% 60%,
          0 60%,
          10% 45%,
          45% 45%,
          45% 10%,
          55% 10%,
          55% 45%,
          90% 45%,
          90% 55%,
          55% 55%,
          55% 90%,
          45% 90%,
          45% 55%,
          10% 55%
        );
        display: flex;
        justify-content: center;
        align-items: center;
        background: white;
        width: 20px;
        height: 20px;
        cursor: pointer;
      }
    }
  }
}
.promptData {
  position: fixed;
  top: 40%;
  left: 50%;
  transform-origin: center center;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 300px;
  padding: 20px;
  background: white;
  color: black;
  z-index: 999;
  h3 {
    font-size: 16px;
    font-weight: 600;
  }
  span {
    font-size: 12px;
  }
  h3,
  span {
    display: block;
  }
}
//给弹窗添加渐入和渐出动画
.prompt-enter-active,
.prompt-leave-active {
  transition: opacity 0.3s ease;
}
.prompt-enter-from,
.prompt-leave-to {
  opacity: 0;
}
//给弹窗添加滑动动画
.slide-up-enter-active,
.slide-up-leave-active {
  transition: transform 0.3s ease, opacity 0.3s ease;
}
.slide-up-enter-from,
.slide-up-leave-to {
  transform: translateY(30px);
  opacity: 0;
}
</style>
